package com.chen.model;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.view.json.MappingJacksonJsonView;

import com.chen.services.BaseService;
import com.chen.services.BaseServiceImpl;
import com.chen.util.BaseUtil;
import com.chen.util.ELFuncUtil;
import com.chen.util.ExcelUtil;

@SuppressWarnings("unchecked")
@Controller
public class BaseController {
	private List<Base> bases = new ArrayList();
	private BaseService baseService = new BaseServiceImpl();
	
	@RequestMapping(value="/querybase", method=RequestMethod.GET)
	public String query(@Valid Base base, Model model) throws Exception {
		System.out.println(base.getJob_No());
		bases.clear();
		bases = baseService.query();
		model.addAttribute("bases",bases);
		return "basequery";
	}
	@RequestMapping(value="/querydetail", method=RequestMethod.GET)
	public String querydetails(@Valid Base base, Model model) throws Exception {
		bases.clear();
		int c = base.getnP().intValue();
		String condition = new String();
		switch (c) {
		case 1:
			condition = "job_no";
			break;
		case 2:
			condition = "name";
			break;
		case 3:
			condition = "bclass";
			break;	
		case 4:
			condition = "job_no2";
			break;	
		default:
			break;
		}
		bases = baseService.queryByNo(condition,base.getJob_No());
		model.addAttribute("bases",bases);
		return "basedetail";
	}
	
	@RequestMapping(value="/queryByNo", method=RequestMethod.GET)
	public String queryByNo(@Valid Base base, Model model) throws Exception {
		System.out.println(base.getJob_No());
		bases.clear();
		int c = base.getnP().intValue();
		String condition = new String();
		switch (c) {
		case 1:
			condition = "job_no";
			break;
		case 2:
			condition = "name";
			break;
		case 3:
			condition = "bclass";
			break;	
		case 4:
			condition = "job_no2";
			break;		
		default:
			break;
		}
		bases = baseService.queryByNo(condition,base.getJob_No());
		model.addAttribute("bases",bases);
		return "basequery";
	}
	
	@RequestMapping(value="/save", method=RequestMethod.GET)
	public  ModelAndView updateByNo(HttpServletRequest request,HttpServletResponse response) throws Exception {
		//System.out.println("save****************************************************"+base.get(1).toString());
		//bases.clear();
		ModelAndView mav = new ModelAndView();
		MappingJacksonJsonView view = new MappingJacksonJsonView();
		//bases = baseService.queryByNo(base.getJob_No());
		String id = request.getParameter("id");
		String job_no2 = request.getParameter("job_no2");
		String name = request.getParameter("name");
		String postion = request.getParameter("position");
		String bclass = request.getParameter("bclass");
		
		Double pf = BaseUtil.isDouble(request.getParameter("pf"));
		Double nh = BaseUtil.isDouble(request.getParameter("nh"));
		Double oh = BaseUtil.isDouble(request.getParameter("oh"));
		Double sh = BaseUtil.isDouble(request.getParameter("sh"));
		Double hh = BaseUtil.isDouble(request.getParameter("hh"));
		Double bonus = BaseUtil.isDouble(request.getParameter("bonus"));
		Double nssa = BaseUtil.isDouble(request.getParameter("nssa"));
		Double nec = BaseUtil.isDouble(request.getParameter("nec"));
//计算项		
		Double	np= nh*pf;
		Double	op=oh*pf;
		Double	sp=sh*pf;
		Double	hp=hh*pf;
		Double	gross=ELFuncUtil.getGross(nh, oh, sh, hh, pf, bonus);
		Double	paye=ELFuncUtil.getPaye(nh, oh, sh, hh, pf, bonus, nssa, nec, 250.0, 0.2);
		Double	aids=paye*0.3;
		Double	total=nssa+paye+aids+nec;
		Double	net=gross-total;
////////////////////////////////////////		
		Base base = new Base();
		base.setJob_No(id);
		if(BaseUtil.isBlank(job_no2)){
			job_no2="";
		}
		base.setJob_No2(job_no2);
		base.setName(name);
		base.setPostion(postion);
		base.setBclass(bclass);
		
		base.setPayFactor(pf);
		base.setnH(nh);
		base.setoH(oh);
		base.setsH(sh);
		base.sethH(hh);
		base.setBonus(bonus);
		base.setnSSA(nssa);
		base.setnEC(nec);
//计算项		
		base.setnP(np);
		base.setoP(op);
		base.setsP(sp);
		base.sethP(hp);
		base.setGross(gross);
		base.setPaye(paye);
		base.setaIDS(aids);
		base.setTotal(total);
		base.setNet(net);
////////////////////////////////////////		
		baseService.save(base);
		 Map attributes = new HashMap();
	        attributes.put("success", Boolean.TRUE);
	        view.setAttributesMap(attributes);
	        mav.setView(view);
	        return mav;
	}
	@RequestMapping(value="/delete", method=RequestMethod.GET)
	public  ModelAndView delete(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		MappingJacksonJsonView view = new MappingJacksonJsonView();
		String id = request.getParameter("id");
		Base base = new Base();
		base.setJob_No(id);
		baseService.delete(base);
		 Map attributes = new HashMap();
	        attributes.put("success", Boolean.TRUE);
	        view.setAttributesMap(attributes);
	        mav.setView(view);
	        return mav;
	}		
	@RequestMapping(value="/toexcel", method=RequestMethod.GET)
	public  ModelAndView toexcel(HttpServletRequest request,HttpServletResponse response) throws Exception {
		ModelAndView mav = new ModelAndView();
		MappingJacksonJsonView view = new MappingJacksonJsonView();
		
		int c = Integer.parseInt(request.getParameter("id"));
		String where = request.getParameter("v");
		String condition = new String();
		switch (c) {
		case 1:
			condition = "job_no";
			break;
		case 2:
			condition = "name";
			break;
		case 3:
			condition = "bclass";
			break;	
		case 4:
			condition = "job_no2";
			break;	
		default:
			break;
		}
		bases = baseService.queryByNo(condition,where);
		ExcelUtil eu = new ExcelUtil();
		eu.toexcel(bases);
		Map attributes = new HashMap();
		attributes.put("success", Boolean.TRUE);
		view.setAttributesMap(attributes);
		mav.setView(view);
		return mav;
	}		
}
